from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
import excel_info_sql
import xigua_selenium.biz_mysql
list1 = excel_info_sql.getConditions()

def novel_test(Novel_name,insert_time,Chapter,Extension_name,Platform):

    driver = webdriver.Chrome(r'D:\Program Files\python3.63\chromedriver.exe')
    # 进入 西瓜
    driver.get("https://www.koread.cn/admin/notice?ref=addtabs")
    driver.maximize_window()
    driver.implicitly_wait(10)

    # 定位账号
    driver.find_element_by_name('username').clear()
    driver.find_element_by_name('username').send_keys('{0}'.format(""))

    # 定位密码
    time.sleep(0.5)
    driver.find_element_by_name('password').clear()
    driver.find_element_by_name('password').send_keys('{0}'.format(''))

    # 点击登录
    driver.find_element_by_xpath('//div[5]/button').click()

    #点击小说推广
    try:
        time.sleep(1)
        WebDriverWait(driver,15).until(
            EC.element_to_be_clickable((By.XPATH,'//section/ul/li[4]/a'))
        )
        driver.find_element_by_xpath('//section/ul/li[4]/a').click()
    except Exception:
        print("点击小说推广error")
    #点击小说中心
    try:
        WebDriverWait(driver, 15).until(
            EC.element_to_be_clickable((By.XPATH, '//li[4]//li[1]/a'))
        )
        driver.find_element_by_xpath('//li[4]//li[1]/a').click()
    except Exception:
        print("点击小说中心error")

    # for i in range(0, len(list1)):
    #     # 小说名字
    #     Novel_name = list1[i]["Novel_name"]
    #     # 日期
    #     insert_time = list1[i]["date_time"]
    #     # 章节
    #     Chapter = list1[i]["Chapter"]
    #     # 推广名称
    #     Extension_name = list1[i]["Extension_name"]
    #     # 平台名称
    #     Platform = list1[i]["Platform"]
    #点击名称选择搜索框
    time.sleep(1)
    driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="con_375"]/iframe'))
    try:
        WebDriverWait(driver, 15).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="name"]'))
        )
        driver.find_element_by_xpath('//*[@id="name"]').clear()
        # time.sleep(1)
        driver.find_element_by_xpath('//*[@id="name"]').send_keys(Novel_name)
    except Exception:
        print("点击名称选择搜索框error")

    #点击提交
    try:
        WebDriverWait(driver, 15).until(
            EC.element_to_be_clickable((By.XPATH, '//div[9]//button[1]'))
        )
        driver.find_element_by_xpath('//div[9]//button[1]').click()
    except Exception:
        print("点击提交error")

    #点击推广连接
    try:
        time.sleep(1)
        WebDriverWait(driver, 15).until(
            EC.element_to_be_clickable((By.XPATH, '//td[8]/a'))
        )
        driver.find_element_by_xpath('//td[8]/a').click()
    except Exception:
        print("点击推广连接error")

    # 新iframe
    time.sleep(0.5)
    driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="layui-layer-iframe{0}"]'.format(1)))

    # 点击推广链接
    try:
        time.sleep(0.5)
        # WebDriverWait(driver, 15).until(
        #     EC.element_to_be_clickable((By.XPATH, '//div[9]//button[1]'))
        # )
        driver.find_element_by_xpath('//tr[{0}]//a[3]'.format(Chapter)).click()
    except Exception:
        print('点击推广链接error')

    #新Iframe
    time.sleep(0.5)
    driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="layui-layer-iframe1"]'))
    try:
        time.sleep(0.5)
        WebDriverWait(driver, 15).until(
            EC.element_to_be_clickable((By.XPATH, '//*[@id="c-name"]'))
        )
        driver.find_element_by_xpath('//*[@id="c-name"]').clear()
        driver.find_element_by_xpath('//*[@id="c-name"]').send_keys('{0}'.format(insert_time + Extension_name))
    except Exception:
        print("输入渠道名称error")

    # 确认
    try:
        time.sleep(1)
        WebDriverWait(driver, 15).until(
            EC.element_to_be_clickable((By.XPATH, '//button[text()="确定"]'))
        )
        driver.find_element_by_xpath('//button[text()="确定"]').click()
        time.sleep(2)
        url = driver.find_element_by_xpath('//*[@id="add-dialog"]//div[1]/div[2]/div[2]').text
        print(url)
        # 数据入库
        xigua_selenium.biz_mysql.insert_novel_mysql(url=url, Public_platform=Platform, Novel_name=Novel_name, Chapter=Chapter,datetime=insert_time, Extension_name=Extension_name)
    except Exception:
        print("确认出错")
    # # 退出当前iframe
    # driver.switch_to.default_content()
    #
    # # 取消
    # try:
    #     time.sleep(1)
    #     driver.switch_to_frame(driver.find_element_by_xpath('//*[@id="con_375"]/iframe'))
    #     # time.sleep(2)
    #     driver.find_element_by_xpath('//*[@id="layui-layer{0}"]/span[1]/a[3]'.format(i+1)).click()
    # except Exception:
    #     print("取消1")
    #
    # driver.switch_to.default_content()
    # time.sleep(2)
    driver.quit()
#
# if __name__== "__main__":
#     novel_test()






